package com.yyk.yiliao.ui.trade;

import android.graphics.Color;
import android.os.Bundle;
import com.google.android.material.textfield.TextInputEditText;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.listener.ToolbarListenter;
import com.yyk.yiliao.util.BtnToEditListenerUtil;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.ToastUtil;
import com.yyk.yiliao.util.glide.GlideUtils;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.bean.OrderPhonecardInfo;
import com.yyk.yiliao.bean.OrderSupcardInfo;
import com.yyk.yiliao.util.rx.RxUtils;

import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 添加银行卡
 */
public class AddBankcardActivity extends BaseActivity {

    public static AddBankcardActivity intance;
    @BindView(R.id.tv_title) //标题
            TextView tvTitle;
    @BindView(R.id.bt_next) //下一步
            Button btNext;
    @BindView(R.id.et_name)//姓名
            TextInputEditText etName;
    @BindView(R.id.et_card)//银行卡号
            TextInputEditText etCard;
    @BindView(R.id.iv_cardimg)
    ImageView ivCardimg;
    @BindView(R.id.tv_cardname)
    TextView tvCardname;
    @BindView(R.id.ll_card)
    LinearLayout llCard;

    private int mark; //1.支持 银行

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_bankcard;
    }

    @Override
    public void initView() {
        initToolbar();
        initTextView();
        initEdittext();
        initButton();
        etName.setText(Hawk.get("real_name") + "");

    }

    private void initEdittext() {
        etCard.addTextChangedListener(new TextWatcher() {

            private int length;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                length = s.length();

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (length == 9) {
                    llCard.setVisibility(View.VISIBLE);
                    Map<String, String> map = new TreeMap<>();
                    map.put("cnumber", String.valueOf(s));
                    map.put("sign", Md5Util2.createSign(map));
                    RxUtils.getInstance().retrofit.create(ApiService.class).postOrderSupcard(map)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .unsubscribeOn(Schedulers.io())
                            .subscribe(new Subscriber<OrderSupcardInfo>() {
                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable e) {
//                                    ToastUtil.showShort(mActivity, "异常");

                                }

                                @Override
                                public void onNext(OrderSupcardInfo info) {
                                    //正确输入不打印出来值
                                    Logger.e("绑卡银行信息第一步" + info.toString());
                                    if (info.getCode() == 1) {
                                        OrderSupcardInfo.DataBean data = info.getData();
                                        String img = data.getImg();
                                        String name = data.getName();
                                        String state = data.getState();
                                        GlideUtils.loadImage(mActivity, ApiService.BASE_URL + img, ivCardimg);
                                        tvCardname.setText(name + state);
                                        mark = data.getMark();
                                    } else {
                                        llCard.setVisibility(View.INVISIBLE);
                                    }
                                }
                            });
                } else if (length < 9) {
                    llCard.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    private void initButton() {
        BtnToEditListenerUtil.getInstance().setBtn(btNext).setBackgroud(R.color.blue_51, R.color.blue_89)
                .addEditView(etName)
                .addEditView(etCard)
                .build();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("添加银行卡");
        setMainTitleRightText("合作银行", new ToolbarListenter() {
            @Override
            public void onClickListenter() {
                startAct(SelectYinhangCardActivty.class);
            }
        });

    }

    private void initTextView() {
        //更改颜色 Android中TextView中部分文字有不同颜色的实现 - 丿灬安之若死 - CSDN博客  https://blog.csdn.net/mp624183768/article/details/78722298
        SpannableString spannableString = new SpannableString("请添加账户本人银行卡");
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#148DFF")), spannableString.length() - 7, spannableString.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvTitle.setText(spannableString);
    }

    @Override
    public void initData() {
        intance = this;
    }


//    int REQUEST_CODE = 1;
//
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode == 2) {
//            if (requestCode == REQUEST_CODE) {
//                Bundle extras = data.getExtras();
//                String name = extras.getString("name");
//                bankid = extras.getInt("bankid");
//                etCardname.setText(name);
//
//            }
//        }
//
//    }

    @OnClick({R.id.bt_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_next:
                if (mark == 1) {
                    addBankcard();
                } else {
                    ToastUtil.showShort(mActivity, "该银行不支持，请查看合作银行");
                }

                break;
        }
    }


    private void addBankcard() {
        String name = etName.getText().toString().trim();
        String numberu = etCard.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtil.showShort(mActivity, "请输入真实姓名");
            return;
        }
        if (TextUtils.isEmpty(numberu)) {
            ToastUtil.showShort(mActivity, "银行卡号不能为空");
            return;
        }

        Map<String, String> map = new TreeMap<>();
        map.put("cnumber", numberu);
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postOrderPhonecard(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<OrderPhonecardInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showShort(mActivity, "异常");

                    }

                    @Override
                    public void onNext(OrderPhonecardInfo info) {
                        //正确输入不打印出来值
                        Logger.e("绑定银行卡第二步" + info.toString());
                        if (info.getCode() == 1) {
                            Bundle bundle = new Bundle();
                            bundle.putString("cardname", info.getData().getName());
                            bundle.putString("card", info.getData().getNumberu());
                            startAct(AddBankcard2Activity.class, bundle);
                        } else {
                            ToastUtil.showShort(mActivity, "请认真填写信息");
                        }
                    }
                });

    }


}
